#include <iostream>
#include "UnidirectionLinkedList.hpp"

using namespace std;

class Solution
{
public:
    ListNode *reverseBetween(ListNode *head, int left, int right)
    {
        left -= 1;
        right -= 1;
        if (left == right)
            return head;
        ListNode *leftBefore = nullptr;
        ListNode *leftBegin = head;
        for (int i = 0; i < left; i++)
        {
            leftBefore = leftBegin;
            leftBegin = leftBegin->next;
        }
        ListNode *last = leftBegin;
        ListNode *cur = leftBegin->next;
        ListNode *next = nullptr;
        for (int i = 0; i < right - left; i++)
        {
            next = cur->next;
            cur->next = last;
            last = cur;
            cur = next;
        }
        if (leftBefore != nullptr)
            leftBefore->next = last;
        leftBegin->next = cur;
        return leftBefore == nullptr ? last : head;
    }
};
int main()
{
    cout << "Code92" << endl;
    Solution s;
    int arr[] = {1, 2};
    ListNode *head = CreateLinkedList(arr, 2);
    ListNode *ret = s.reverseBetween(head, 1, 2);
    PrintLinkedList(ret);
    return 0;
}